//tbGerenrateTemplate.controller.ts


import { Inject, Controller, Get, Query, Post, Body, App, All } from "@midwayjs/core";
import { Context } from "@midwayjs/koa";
import { HttpService } from "@midwayjs/axios";
import { Application } from "../interface.js";
import { failres, okres } from "../config/resUtil.js";
import { InjectEntityModel } from "@midwayjs/typeorm";
import { Repository } from "typeorm";
import { tbGerenrateTemplateEntity } from "../entity/tbGerenrateTemplate.entity.js";
import { BaseDto } from "../models/Base.dto.js";
import { getSkip } from "../utils/pageUtil.js";
import { deepClone } from "../utils/lodashUtil.js";


@Controller("/api/tb_gerenrate_template")
export class tbGerenrateTemplateController {
  @Inject()
  ctx: Context;
  @App("koa")
  app: Application;

  @InjectEntityModel(tbGerenrateTemplateEntity, "default")
  tbGerenrateTemplateModel: Repository<tbGerenrateTemplateEntity>;

  //find
  @Post("/getlist")
  async getlist(@Body() body: tbGerenrateTemplateEntity & BaseDto) {
    var query = this.tbGerenrateTemplateModel
      .createQueryBuilder("tb_gerenrate_template").select();
    query = query.where("1=1");


    if (body?.id) {
      query = query.andWhere(`id = :id`, {
        id: body?.id
      });
    }


    if (body?.name) {
      query = query.andWhere(`name = :name`, {
        name: body?.name
      });
    }


    if (body?.desccontent) {
      query = query.andWhere(`desccontent = :desccontent`, {
        desccontent: body?.desccontent
      });
    }




    var result3 = await query.skip(getSkip(body)).take(body.pageSize).getManyAndCount();
    var datalist = (result3?.[0] || []) as unknown as tbGerenrateTemplateEntity[];
    return okres(datalist, { total: result3?.[1] });
  }

  //findone
  @Get("/getbyid")
  async getitem(@Query("id") id) {
    var query = this.tbGerenrateTemplateModel
      .createQueryBuilder("tb_gerenrate_template").select();
    query = query.andWhere(`id = :id`, { id: id });
    var result3 = await query.getOne();
    return okres(result3);
  }

  //add
  @Post("/add")
  async add(@Body() body: tbGerenrateTemplateEntity & BaseDto) {
    var temData = new tbGerenrateTemplateEntity(body);
    delete temData.id;
    var result = await this.tbGerenrateTemplateModel.save(temData);
    return okres(result);
  }

  //update
  @Post("/update")
  async update(@Body() body: tbGerenrateTemplateEntity & BaseDto) {
    var params = deepClone(body);
    var id = params?.id;
    delete params.id;
    var result = await this.tbGerenrateTemplateModel.update(id, params);
    return okres(result);
  }

  //delbyid
  @Get("/delbyid")
  async deletbyid(@Query("id") id) {
    var result = await this.tbGerenrateTemplateModel.delete(id);
    return okres(result);
  }


}
